package com.cn.byjk.config;

import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * @author zhangdianzhi
 * @version v1.0
 * @description rabbitmq配置类
 * @date 2022/1/6 11:47
 */
@Configuration
public class RabbitConfig {

        @Value("${spring.rabbitmq.host}")
        private String host;

        @Value("${spring.rabbitmq.port}")
        private String port;

        @Value("${spring.rabbitmq.username}")
        private String username;

        @Value("${spring.rabbitmq.password}")
        private String password;

        @Value("${spring.rabbitmq.publisher-confirms}")
        private Boolean publisherConfirms;

        @Value("${spring.rabbitmq.virtual-host}")
        private String virtualHost;

        //创建工厂连接
        @Bean
        public ConnectionFactory connectionFactory(){
            CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
            connectionFactory.setAddresses(this.host);
            connectionFactory.setUsername(this.username);
            connectionFactory.setPassword(this.password);
            connectionFactory.setVirtualHost(this.virtualHost);
            connectionFactory.setPublisherConfirms(this.publisherConfirms); //必须要设置
            return connectionFactory;
        }

        //rabbitmq的模板配置
        @Bean
        @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) //必须是prototype类型
        public RabbitTemplate rabbitTemplate() {
            RabbitTemplate template = new RabbitTemplate(this.connectionFactory());
            //template.setConfirmCallback(); 设置消息确认
            //template.setReturnCallback();
            return template;
        }

}
